﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Wokr2
{
    public partial class Regist : Form
    {
  
        public Regist()
        {
            InitializeComponent();
        }

        private void btt_Regist_Click(object sender, EventArgs e)
        {
  
            String regist_Username = tB_Regist_Username.Text;
            String regist_Password1 = tB_Regist_Password1.Text;
            string regist_Password2 = tB_Regist_Password2.Text;
            string regist_uid = tb_Regist_Un.Text;
            string regist_sex = tb_Regist_Sex.Text;
            string regist_eid = tb_Regist_Eid.Text;

  
            if (regist_Username == "" || regist_Password1 == "" || regist_Password2 == "")
            {
                MessageBox.Show("请输入用户名和密码！");
                return;
            }
 
            if (!regist_Password1.Equals(regist_Password2))
            {
                MessageBox.Show("两次输入的密码不一致，请重新输入！");
                return;
            }



            string sqlcon = @"Data Source=LAPTOP-LER1MVBI\SQLEXPRESS;Initial Catalog=电子族谱系统;uid=sa;pwd=zxcvbnm123";
            String sqlcom = " select User_id from Users where User_id='" + regist_Username + "'";
            SqlConnection con = new SqlConnection(sqlcon);
            SqlCommand com = new SqlCommand(sqlcom, con);
            con.Open();

            SqlDataReader reader = com.ExecuteReader();
            if (reader.Read())
            {
                MessageBox.Show("用户名已存在，请重新输入用户名！");
                return;
            }
            reader.Close();

            string sqlcon1 = @"Data Source=LAPTOP-LER1MVBI\SQLEXPRESS;Initial Catalog=电子族谱系统;uid=sa;pwd=zxcvbnm123";
            String sqlcom1 = " select Egenealogy_id from Egenealogy where Egenealogy_id='" + tb_Regist_Eid.Text + "'";
            SqlConnection con1 = new SqlConnection(sqlcon1);
            SqlCommand com1 = new SqlCommand(sqlcom1, con1);
            con1.Open();

            SqlDataReader reader1 = com.ExecuteReader();
            if (reader1.Read())
            {
                MessageBox.Show("族谱不存在");
                return;
            }
            else
            {
                
            }
            reader1.Close();




            String sqlcom2 = " insert into Users(User_id,User_name,User_pwd,User_sex,User_genealogy) values('" + regist_Username + "','"+ regist_uid + "','" + regist_Password1 + "','"+ regist_sex + "','"+ regist_eid + "')";
            SqlCommand com2 = new SqlCommand(sqlcom2, con);
            int eq=com2.ExecuteNonQuery();    
            if (eq!=0)
            {
                MessageBox.Show("注册成功！将跳转到登录页面！");
                this.Close();
            }
            con.Close();

        }

        


        private void tB_Regist_Username_TextChanged(object sender, EventArgs e)
        {

        }

        private void Regist_Load(object sender, EventArgs e)
        {

        }

        private void pctrB_Regist_Image_Click(object sender, EventArgs e)
        {

        }

        private void Label5_Click(object sender, EventArgs e)
        {

        }

        private void Label1_Click(object sender, EventArgs e)
        {

        }

        private void Label6_Click(object sender, EventArgs e)
        {

        }
    }
}
